﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

using Ivy.SiteGeneralBasic.Entity;
using Ivy.SiteGeneralBasic.IBusinessLogic;
using Ivy.SiteGeneralBasic.DataAccess;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common.Framework.EmailUtility;

namespace Ivy.SiteGeneralBasic.BusinessLogic
{
    public class SiteGeneralBasicBus : ISiteGeneralBasic
    {
        #region  Variable & constant
        // All constants and local variable should be declared here
        // TRY NOT TO CREATE PUBLIC VARIABLES INSTEAD USE PROPERTIES FOR THAT PURPOSE.
        // Variable naming convention :- 
        // The naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Naming convention for all local variable should start with underscore "_"
        // Naming convention for all local constant should start with "_The"
        // Example :    string _CustomerCode = string.Empty;
        // Example :    const string _TheDefaultSmtpServer = "DefaultSmtpServer";
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion  Variable & constant

        #region Constructor & Destructor
        // Cut and Copy the default constructor for the class inside this region.
        // Generally not required in UI layer.
        // Example:
        // public SampleClassTemplate()
        // {

        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Constructor & Destructor

        #region Properties
        // Any value/variable that needs to be exposed outside the class we need to create a property.
        // Avoid creating public variables rather create public properties.
        // UI layer may have public custom properties. these are very handy for User Controls.
        // Property naming convention :- Properties will be exposed outside the class, 
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // All properties should have a corresponding local variable 
        // and the property name and local variable name should match.
        // Example :
        // public string CustomerCode
        // {
        //     get
        //     {
        //         return _CustomerCode;
        //     }
        //     set
        //     {
        //         _CustomerCode = value;
        //     }
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Properties

        #region Events
        // Generally UI layer will have public events.
        // Cut and Copy the default generated events for the class inside this region.
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Events

        #region Public Methods
        // Any method that needs to be exposed outside the class will be created inside this region.
        // Public Method naming convention :- Public Method will be exposed outside the class, 
        // The input and output parameters are also going to be exposed outside the class
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Example :
        // public bool RegisterNewCustomer(Customer Customer)
        // {
        //     bool IsCustomerDataSaved = false;
        //     //TO DO logic to save data
        //     IsCustomerDataSaved = InsertCustomerDataUsingDao(Customer);
        //     return IsCustomerDataSaved;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================

        #region Subscription
        public bool RegisterNewSubscription(Subscription SubscriptionEntity)
        {
            bool IsOperationSuccessful = false;

            SubscriptionDao lSubscriptionDao = new SubscriptionDao();
            IsOperationSuccessful = lSubscriptionDao.Insert(SubscriptionEntity);
            SendSubscriptionNotificationEmail(SubscriptionEntity.SubscribUserMailID, SubscriptionEntity.SubscribItemDescription);
            return IsOperationSuccessful;

        }

        public Subscription GetByEmail(string SubscribUserMailID)
        {
            SubscriptionDao lSubscriptionDao = new SubscriptionDao();
            Subscription lSubscription;
            lSubscription = lSubscriptionDao.GetByEmailID(SubscribUserMailID);

            if (lSubscription == null)
            {
                return null;
            }
            else
            {
                return lSubscription;
            }
        }

        public List<Subscription> GetAllSubscription()
        {
            List<Subscription> lSubscriptionList = new List<Subscription>();
            SubscriptionDao lSubscriptionDao = new SubscriptionDao();
            lSubscriptionList = lSubscriptionDao.GetAll();

            return lSubscriptionList;
        }

        public bool SendSubscriptionNotificationEmail(string SubscriberEmailId, string SubscriptionItemDescription)
        {
            bool IsOperationSuccessful = false;

            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "FrameworkIvy - Subscription request received notification";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style5 { font-size: large; font-weight: bold; color: #006600; } .style6 { text-align: left; font-size: small; color: #006600; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <h1 class=""style1""> Subscription Request Notification! </h1> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> FrameworkIvy has received a Subscription request at the web site.</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> Subscriber Email : <span class=""style5"">" + SubscriberEmailId + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> Subscription Item : <span class=""style5"">" + SubscriptionItemDescription + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> FrameworkIvy Administrator -  Notification System </td> </tr> ";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2015&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";


            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);

            return IsOperationSuccessful;
        }
        #endregion Subscription

        #region Feedback
        public bool SubmitFeedBack(FeedbackUser feedbackUserEntity, Feedback feedbackEntity)
        {
            bool IsOperationSuccessful = false;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            FeedbackDao lFeedbackDao = new FeedbackDao();
            FeedbackUser lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);

            if (lFeedbackUser == null)
            {
                //feedbackUserEntity.RegisteredUserkey = GetUserKey(feedbackUserEntity.FeedbackUserEmail);
                lFeedbackUserDao.Insert(feedbackUserEntity);
                lFeedbackUser = lFeedbackUserDao.GetByEmail(feedbackUserEntity.FeedbackUserEmail);
                feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
            }
            else
            {
                feedbackEntity.FeedbackUserKey = lFeedbackUser.FeedbackUserKey;
            }
            lFeedbackDao.Insert(feedbackEntity);
            IsOperationSuccessful = true;
            string lFeedbackText = StringUtility.ConvertHtmlToString(feedbackEntity.FeedbackText);
            SendFeedbackNotificationEmail(lFeedbackUser.FeedbackUserName, lFeedbackUser.FeedbackUserEmail, lFeedbackText);


            return IsOperationSuccessful;
        }
        public bool CreateFeedBackUser(FeedbackUser FeedbackUser)
        {
            bool IsOperationSuccessful = false;

            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            lFeedbackUserDao.Insert(FeedbackUser);
            IsOperationSuccessful = true;

            return IsOperationSuccessful;
        }

        public bool SendFeedbackNotificationEmail(string FeedbackUserName, string FeedbackUserEmailId, string FeedbackText)
        {
            bool IsOperationSuccessful = false;

            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "FrameworkIvy - Feedback received notification";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style5 { font-size: large; font-weight: bold; color: #006600; } .style6 { text-align: left; font-size: small; color: #006600; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <h1 class=""style1""> Feedback  Notification! </h1> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> FrameworkIvy has received a Feedback at the web site.</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> <span class=""style3"">User Name : </span><span class=""style5"">" + FeedbackUserName + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> User Email : <span class=""style5"">" + FeedbackUserEmailId + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> Feedback Details:</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style6"">" + FeedbackText + "</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> FrameworkIvy Administrator -  Notification System </td> </tr> ";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2015&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);

            return IsOperationSuccessful;
        }

        public List<Feedbacks> GetAllUserFeedback()
        {
            List<Feedbacks> lFeedbackList = new List<Feedbacks>();

            FeedbackDao lFeedbackDao = new FeedbackDao();
            lFeedbackList = lFeedbackDao.GetAllUserFeedback();

            return lFeedbackList;
        }

        public List<Feedbacks> GetAllUserContactRequest()
        {
            List<Feedbacks> lFeedbackList = new List<Feedbacks>();

            FeedbackDao lFeedbackDao = new FeedbackDao();
            lFeedbackList = lFeedbackDao.GetAllUserContactRequest();

            return lFeedbackList;
        }

        public FeedbackUser GetByFeedbackUserEmailId(string FeedbackUserEmail)
        {
            FeedbackUserDao lFeedbackUserDao = new FeedbackUserDao();
            FeedbackUser lFeedbackUser;
            lFeedbackUser = lFeedbackUserDao.GetByEmail(FeedbackUserEmail);
            if (lFeedbackUser == null)
            {
                return null;
            }
            else
            {
                return lFeedbackUser;
            }
        }

        public Feedbacks GetByFeedbackUserByKey(int FeedbacksKey)
        {
            FeedbackDao lFeedbackDao = new FeedbackDao();
            Feedbacks lFeedbacks;
            lFeedbacks = lFeedbackDao.GetByKey(FeedbacksKey);
            if (lFeedbacks == null)
            {
                return null;
            }
            else
            {
                return lFeedbacks;
            }
        }
        #endregion Feedback

        #endregion Public Methods

        #region Private Methods
        // All local class level methods will be inside this region. Mostly these are helper methods.
        // Private Method naming convention :- The naming should be self explanatory, 
        // make it long to explain the purpose and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Input parameter will have a "p" as prefix. 
        // This will help the parameter variable that the value is originated outside this private method.
        // Example :
        // public bool InsertCustomerDataUsingDao(Customer pCustomer)
        // {
        //     bool IsCustomerDataInsertedSuccessfully = false;
        //     //TO DO logic to save data
        //     //....
        //     //....
        //     return IsCustomerDataInsertedSuccessfully;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Private Methods
    }
}
